package com.neuedu.controller;

import com.neuedu.dao.DeptDao;
import com.neuedu.entity.Dept;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Objects;

/**
 * http://localhost:8080/web05/dept?_type=list
 */
@WebServlet(urlPatterns = "/dept")
public class DeptController extends HttpServlet {
    
    private DeptDao deptDao = new DeptDao();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //针对的post
        req.setCharacterEncoding("utf-8");


        //有具体的业务代码 CRUD
        String type = req.getParameter("_type");
        //查询列表
        if(Objects.isNull(type) || "list".equals(type)){
            this.queryList(req,resp);
        }
        //新增
        else if("save".equals(type)){
            this.save(req,resp);
        }
        //修改q=前查询
        else if("toAdd".equals(type)){
            this.toAdd(req,resp);
        }
        //修改q=前查询
        else if("toEdit".equals(type)){
            this.toEdit(req,resp);
        }
        //修改
        else if("update".equals(type)){
            this.update(req,resp);
        }

        //删除
        else if("deleteById".equals(type)){
            this.deleteById(req,resp);
        }else if("ajaxDeleteById".equals(type)){
            this.ajaxDeleteById(req,resp);
        }


    }

    /**
     * 使用ajax的方式删除
     * @param req
     * @param resp
     */
    private void ajaxDeleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取主键
        String deptno = req.getParameter("deptno");

        //删除
        int count = deptDao.deleteById(Integer.parseInt(deptno));

        //响应json数据  true
        String json = "{'status':"+(count>0)+",'msg':'失败原因'}";

        //发送的数据类型
        resp.setContentType("application/json;charset=utf-8");


        //发送数据
        PrintWriter out = resp.getWriter();
        out.write(json);
        out.flush();
        out.close();
    }

    //内部跳转 是可以访问  Web_INF目录
    private void toAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.getRequestDispatcher("/WEB-INF/business/dept/dept_add.jsp").forward(req,resp);
    }


    /**
     * 根据主键删除一条
     * @param req
     * @param resp
     */
    private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取主键
        String deptno = req.getParameter("deptno");

        //删除
        int count = deptDao.deleteById(Integer.parseInt(deptno));
        //重定向 跳转到查询List
        resp.sendRedirect(req.getContextPath()+"/dept?_type=list");
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        //设置请求编码

        String deptno = req.getParameter("deptno");
        String dname = req.getParameter("dname");
        String loc = req.getParameter("loc");

        Dept dept = new Dept(
                Integer.parseInt(deptno),
                dname,
                loc
        );

        DeptDao deptDao = new DeptDao();
        int update = deptDao.updateById(dept);

        //重定向 list
        //获取上下文
        String contextPath = req.getContextPath();
        resp.sendRedirect(contextPath+"/dept?_type=list");
    }

    private void toEdit(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String where  = " where deptno = ?";
        String deptno = request.getParameter("deptno");
        Dept dept = deptDao.selectOne(where, deptno);

        //dept 设置到request
        request.setAttribute("dept",dept);

        //内部跳转到修改页面
        request.getRequestDispatcher("/WEB-INF/business/dept/dept_edit.jsp").forward(request,response);

    }

    private void save(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String deptno = req.getParameter("deptno");
        String dname = req.getParameter("dname");
        String loc = req.getParameter("loc");

        Dept dept = new Dept(
                Integer.parseInt(deptno),
                dname,
                loc
        );

        DeptDao deptDao = new DeptDao();
        int insert = deptDao.insert(dept);

        //重定向 list
        //获取上下文
        String contextPath = req.getContextPath();
        resp.sendRedirect(contextPath+"/dept?_type=list");
    }

    private void queryList(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {


        int size = 5;
        int pageNo = 1;

        String paramPageNo = request.getParameter("pageNo");
        String paraSize = request.getParameter("size");
        if(!Objects.isNull(paramPageNo)){
            pageNo = Integer.parseInt(paramPageNo);
        }
        if(!Objects.isNull(paraSize)){
            size = Integer.parseInt(paraSize);
        }


        String where  = "where 1=1   ";
        long total  = deptDao.selectCount(where);
        List<Dept> deptList = deptDao.selectPage(where,pageNo,size);

         long pages = total%size== 0 ?total/size :  (total/size)+1;


        //把depts交给 jsp使用渲染  列表
        //通过 request共享数据
        request.setAttribute("deptList",deptList);
        request.setAttribute("pages",pages);
        request.setAttribute("size",size);
        request.setAttribute("pageNo",pageNo);
        request.setAttribute("total",total);


        //src/main/webapp/business/dept/dept_list.jsp
        request.getRequestDispatcher("/WEB-INF/business/dept/dept_list.jsp").forward(request,response);

    }


    /**
     * 输出响应内容(html)
     * @param response
     * @param html
     */
    public void out(HttpServletResponse response ,String html) throws IOException {
        response.setContentType("text/html;charset=utf8");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        out.write(html);
        out.flush();
        out.close();
    }


}
